package ro.theneckmaster.usbSync;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class SyncronizeActionModel {

	public enum FileAction {
		DELETE_FOLDER, DELETE_FILE, CREATE_FOLDER, COPY_FILE, REPLACE_FILE
	};

	private int id;
	private FileAction action;
	private String sourcePath;
	private String destPath;
	private String actionMessage;
	private String actionName;

	public SyncronizeActionModel(int id, FileAction action, String sourcePath,
			String destPath) {
		super();
		this.id = id;
		this.action = action;
		this.sourcePath = sourcePath;
		this.destPath = destPath;
		
		switch (action) {
		case DELETE_FOLDER:
			actionMessage = "Delete folder: "+sourcePath;
			actionName = "Delete Folder";
			break;
		case DELETE_FILE:
			actionMessage = "Delete file: "+sourcePath;
			actionName = "Delete File";
			break;
		case CREATE_FOLDER:
			actionMessage = "Create Folder: "+destPath;
			actionName = "Create Folder";
			break;
		case COPY_FILE:
			actionMessage = "Copy File: "+sourcePath;
			actionName = "Copy File";
			break;
		case REPLACE_FILE:
			actionMessage = "Replace File: "+destPath;
			actionName = "Replace File";
			break;

		default:
			break;
	}
		
	}

	public void executeAction() {
		File sourceFile = null;
		if (sourcePath != null) {
			sourceFile = new File(sourcePath);
		}
		File destFile = null;
		if (destPath != null) {
			destFile = new File(destPath);
		}
		switch (action) {
			case DELETE_FOLDER:
				deleteFolderRecursive(sourceFile);
				break;
			case DELETE_FILE:
				sourceFile.delete();
				break;
			case CREATE_FOLDER:
				destFile.mkdirs();
				break;
			case COPY_FILE:
				try {
					copyFile(sourcePath, destPath);
					destFile.setLastModified(sourceFile.lastModified());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			case REPLACE_FILE:
				try {
					destFile.delete();
					copyFile(sourcePath, destPath);
					destFile.setLastModified(sourceFile.lastModified());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
	
			default:
				break;
		}
	}

	public void deleteFolderRecursive(File file) {
		String[] filesAndFolders = file.list();
		for (int i = 0; i < filesAndFolders.length; i++) {
			String fileOrFolder = filesAndFolders[i];
			File subFileOrFolder = new File(file.getAbsolutePath()
					+ File.separator + fileOrFolder);
			deleteFolderRecursive(subFileOrFolder);
		}
		file.delete();
	}

	public static void copyFile(String from, String to) throws IOException {
		FileChannel in = null, out = null;
	     try {          
	          in = new FileInputStream(from).getChannel();
	          out = new FileOutputStream(to).getChannel();
	 
	          long size = in.size();
	          MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
	 
	          out.write(buf);
	 
	     } finally {
	          if (in != null)          in.close();
	          if (out != null)     out.close();
	     }
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getActionMessage() {
		return actionMessage;
	}

	public void setActionMessage(String actionMessage) {
		this.actionMessage = actionMessage;
	}

	public String getActionName() {
		return actionName;
	}

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	public String getSourcePath() {
		return sourcePath;
	}

	public void setSourcePath(String sourcePath) {
		this.sourcePath = sourcePath;
	}

	public String getDestPath() {
		return destPath;
	}

	public void setDestPath(String destPath) {
		this.destPath = destPath;
	}

}
